﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ny.CenterServers.Web.Utils
{
    using Microsoft.AspNetCore.Authentication.JwtBearer;
    using Microsoft.IdentityModel.Tokens;
    using System.IdentityModel.Tokens.Jwt;
    using System.Linq;
    using System.Security.Claims;
    public class MyTokenValidate : ISecurityTokenValidator
        {
            public bool CanValidateToken => true;

            public int MaximumTokenSizeInBytes { get; set; }

            public bool CanReadToken(string securityToken)
            {
                return true;
            }

            public ClaimsPrincipal ValidateToken(string securityToken, TokenValidationParameters validationParameters, out SecurityToken validatedToken)
            {
                ClaimsPrincipal principal;
                try
                {
                    validatedToken = null;
                    //这里需要验证生成的Token
                    /*
    eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoid2FuZ3NoaWJhbmciLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJhZG1pbiwgTWFuYWdlIiwibmJmIjoxNTIyOTI0MDgxLCJleHAiOjE1MjI5MjU4ODEsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAwMCIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAwMCJ9.fa0jDYt_MqHFcwQfsMS30eCsjEwQt_uiv96bGtMQJBE
                    */
                    var token = new JwtSecurityToken(securityToken);
                    //获取到Token的一切信息
                    var payload = token.Payload;
                    var role = (from t in payload where t.Key == ClaimTypes.Role select t.Value).FirstOrDefault();
                    var name = (from t in payload where t.Key == ClaimTypes.Name select t.Value).FirstOrDefault();
                    var issuer = token.Issuer;
                    var key = token.SecurityKey;
                    var audience = token.Audiences;
                    var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
                    identity.AddClaim(new Claim(ClaimTypes.Name, name.ToString()));
                    identity.AddClaim(new Claim(ClaimsIdentity.DefaultRoleClaimType, "admin"));
                    principal = new ClaimsPrincipal(identity);
                }
                catch
                {
                    validatedToken = null;
                    principal = null;
                }
                return principal;
            }
        }
}
